package org.softmed.neodatis.util.provider;

import java.io.File;

import org.neodatis.odb.Configuration;
import org.neodatis.odb.ODB;
import org.softmed.neodatis.util.PrimaryKeyManager;

public class ClientServerProvider implements ODBProvider {

	ServerWrapper serverData;
	String dbName;
	String fileName;

	public ClientServerProvider() {

	}

	public ClientServerProvider(ServerWrapper serverData, String dbName) {
		this.serverData = serverData;
		this.dbName = dbName;
		this.fileName = serverData.getDatabaseFile(dbName);
	}

	@Override
	public void erase() throws Throwable {
		File file = new File(fileName);
		file.delete();
	}

	@Override
	public void refreshClassDefinitions() throws Throwable {
		Configuration.getCoreProvider().resetClassDefinitions();

	}

	@Override
	public ODB getCleanODB() throws Throwable {
		// TODO Auto-generated method stub
		return serverData.getCleanODB(dbName);
	}

	@Override
	public ODB getODB() throws Throwable {
		// TODO Auto-generated method stub
		return serverData.getODB(dbName);
	}

	@Override
	public void restart() throws Throwable {
		// TODO Auto-generated method stub

	}

	@Override
	public void start() throws Throwable {
		// TODO Auto-generated method stub
		serverData.startupServer();

	}

	@Override
	public void stop() throws Throwable {
		serverData.stopServer();
	}

	public ServerWrapper getServerData() {
		return serverData;
	}

	public void setServerData(ServerWrapper serverData) {
		this.serverData = serverData;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	@Override
	public File getDatabaseFile() throws Throwable {
		return new File(fileName);
	}

	@Override
	public PrimaryKeyManager getPrimaryKeyManager() {
		return serverData.getPrimaryKeyManager(dbName);
	}

}
